#include "ProgramManager.hpp"

#include "IdentificatorTable.hpp"
#include "CallingContext.hpp"
#include "CallingInstance.hpp"


std::map < unsigned int, CallingContext* > ProgramManager::_allContext;

IdentificatorTable* ProgramManager::_identificators = NULL;

CallingContext* ProgramManager::_currentContext = NULL;

std::vector < CallingInstance* > ProgramManager::_executionStack;

void
ProgramManager::createCallingContext ( unsigned int index )
{
    if ( _allContext.find ( index ) == _allContext.end () )
    {
        CallingContext* ctxt = new CallingContext;
        ctxt->setName ( index );
        _allContext [ index ] = ctxt;
        _currentContext = ctxt;
    }
}

CallingContext*
ProgramManager::getCallingContext ( unsigned int index )
{
    if ( _allContext.find ( index ) == _allContext.end () )
        return NULL;
    return _allContext [ index ];
}

CallingContext*
ProgramManager::getCurrentContext ()
{
    return _currentContext;
}

CallingInstance*
ProgramManager::getCurrentInstance ()
{
    return _executionStack.back ();
}

unsigned int
ProgramManager::addIdentificator ( const std::string& name )
{
    if ( _identificators == NULL )
        _identificators = new IdentificatorTable;
    return _identificators->addIdentificator ( name );
}

const std::string&
ProgramManager::getNameOf ( const unsigned int index )
{
    if ( _identificators == NULL )
        _identificators = new IdentificatorTable;
    return _identificators->getNameOf ( index );
}

const unsigned int
ProgramManager::getIndexOf ( const std::string& name )
{
    if ( _identificators == NULL )
        _identificators = new IdentificatorTable;
    return _identificators->getIndexOf ( name );
}

std::map < unsigned int, CallingContext* >::const_iterator
ProgramManager::begin ()
{
    return _allContext.begin ();
}

std::map < unsigned int, CallingContext* >::const_iterator
ProgramManager::end ()
{
    return _allContext.end ();
}
